package cn.com.ninng;

import cn.com.ninng.common.ListNode;

import java.util.ArrayList;

/**
 * @author liulekang
 * @date 2021/01/25
 */
public class Problem2 {

    public static void main(String[] args) {
        //243
        ListNode l1 = new ListNode(2,new ListNode(4,new ListNode(3)));
        ListNode l2 = new ListNode(5,new ListNode(6,new ListNode(4)));

        addTwoNumbers(l1,l2);

    }


    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode root = new ListNode(0);
        ListNode r = root;
        int c = 0;
        while(l1 != null || l2 != null || c != 0) {
            int a = l1 != null ? l1.val : 0;
            int b = l2 != null ? l2.val : 0;
            int sum = a + b + c;
            c = sum / 10;

            ListNode sumNode = new ListNode(sum % 10);
            r.next = sumNode;
            r = sumNode;

            if(l1 != null) {
                l1 = l1.next;
            }
            if(l2 != null) {
                l2 = l2.next;
            }
        }

        return root.next;
    }

}
